﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;

namespace Infrastructure.Crosscutting.NetFramework.Result
{
    public class CustomWordResult : ActionResult
    {
        public string FileName { get; set; }

        public string Content { get; set; }

        public string Charset { get; set; }

        public System.Text.Encoding Encode { get; set; }

        public CustomWordResult(string content, string fileName, string charset, System.Text.Encoding encode)
        {
            this.Content = content;
            this.FileName = fileName;
            this.Charset = charset;
            this.Encode = encode;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            HttpContext curContext = HttpContext.Current;
            if (curContext != null)
            {
                curContext.Response.Clear();
                curContext.Response.Charset = this.Charset;
                curContext.Response.ContentType = "application/ms-word";
                curContext.Response.AddHeader("content-disposition", "attachment;filename=" + FileName + ".doc");
                curContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                context.HttpContext.Response.BinaryWrite(this.Encode.GetBytes(this.Content));
                curContext.Response.End();
            }
        }
    }
}
